package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.InventoryBean;
import com.project.bean.InventoryInfoBean;
import com.project.bean.WaitProductBean;
import com.project.mapper.IInventoryInfoMapper;
import com.project.mapper.IInventoryMapper;
import com.project.mapper.IWaitProductMapper;
import com.project.service.IPutOutStorageService;
import com.project.service.IRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PutOutStorageServiceImpl implements IPutOutStorageService {
    @Autowired
    private IInventoryInfoMapper IInventoryInfoMapper;
    @Autowired
    private IRecordService recordService;
    @Autowired
    private IInventoryMapper inventoryMapper;
    @Autowired
    private IWaitProductMapper waitProductMapper;
    @Override
    public InventoryInfoBean findInventoryInfo(String productId) {
        QueryWrapper<InventoryInfoBean> query = new QueryWrapper<>();
        query.eq("productId", productId);
        return IInventoryInfoMapper.selectOne(query);
    }

    @Override
    public boolean updateProductNum(String productId, Integer number) {
        //先将库存中的商品数量减少，并添加出货记录
        QueryWrapper<InventoryInfoBean> query = new QueryWrapper<>();
        query.eq("productId", productId);
        InventoryInfoBean inventoryInfoBean = IInventoryInfoMapper.selectOne(query);

        //减少商品的总数量
        InventoryBean inventoryBean = inventoryMapper.selectById(inventoryInfoBean.getProductNum());
        int inventoryBeanAllNum =  inventoryBean.getSumNum();
        if (inventoryInfoBean !=null){
            Integer productInfoNum = inventoryInfoBean.getProductInfoNum();

            Integer  newProductInfoNum = productInfoNum-number;
            int inventoryBeanAllNumNew = inventoryBean.getSumNum() - number;
            if (newProductInfoNum < 0){
                return false;
            }
            inventoryInfoBean.setProductInfoNum(newProductInfoNum);
            inventoryBean.setSumNum(inventoryBeanAllNumNew);
            IInventoryInfoMapper.updateById(inventoryInfoBean);
            inventoryMapper.updateById(inventoryBean);
            recordService.addInRecord(inventoryInfoBean,number,"出库");
        }

        //再查询wait表中所有商品，先判断是否存在
        QueryWrapper<WaitProductBean> queryWJD = new QueryWrapper<>();
        queryWJD.eq("productId", productId);
        WaitProductBean waitProductJudge = waitProductMapper.selectOne(queryWJD);
        if (waitProductJudge != null) {
            //存在，根据商品id，超找出对应的wait，在其基础上添加数量，并减少库存的数量
            waitProductJudge.setNum(waitProductJudge.getNum()+number);

            waitProductMapper.updateById(waitProductJudge);
            return true;
        }else {
            //不存在，就新添加wait
            WaitProductBean waitProductBean = new WaitProductBean();
            waitProductBean.setProductName(inventoryInfoBean.getProductName());
            waitProductBean.setProductType(inventoryBean.getType());
            waitProductBean.setInPrice(inventoryInfoBean.getPrice());
            waitProductBean.setNum(number);
            waitProductBean.setColor(inventoryInfoBean.getColor());
            waitProductBean.setSize(inventoryInfoBean.getSize());
            waitProductBean.setProductId(productId);
            waitProductMapper.insert(waitProductBean);
            return true;
        }
    }
}
